package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallBookReviewMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.vo.BookReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class LitemallBookReviewService {
    @Autowired
    private LitemallBookReviewMapper bookReviewMapper;
    @Autowired
    private LitemallTopicService topicService;
    @Autowired
    private LitemallGoodsService goodsService;
    @Autowired
    private LitemallGoodsProductService goodsProductService;

    /**
     * 根据不同状态（0：待审核:1：已通过，2：未通过）查询list
     *
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<BookReviewVo> queryList(Integer status, Integer page, Integer limit, String sort, String order) {

        LitemallBookReviewExample example = new LitemallBookReviewExample();
        LitemallBookReviewExample.Criteria criteria = example.or();
        if (status == 1) {
            criteria.andStatusEqualTo(1);
        } else if (status == 2) {
            criteria.andStatusEqualTo(2);
        } else if (status == 3) {
            criteria.andStatusEqualTo(3);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<LitemallBookReview> litemallBookReviews = bookReviewMapper.selectByExample(example);

        List<BookReviewVo> bookReviewVoList = new ArrayList<>();
        for (LitemallBookReview bookReview : litemallBookReviews) {
            BookReviewVo bookReviewVo = new BookReviewVo();
            bookReviewVo.setId(bookReview.getId());
            bookReviewVo.setGoodsProductId(bookReview.getGoodsProductId());
            bookReviewVo.setContent(bookReview.getContent());
            bookReviewVo.setAvatar(bookReview.getAvatar());
            bookReviewVo.setNickname(bookReview.getNickname());
            bookReviewVo.setPicUrl(bookReview.getPicUrl());
            bookReviewVo.setStatus(bookReview.getStatus());
            bookReviewVo.setUserId(bookReview.getUserId());
            bookReviewVo.setAddTime(bookReview.getAddTime());
            bookReviewVo.setUpdateTime(bookReview.getUpdateTime());
            Integer topicId = bookReview.getTopicId();
            if (topicId != null) {
                LitemallTopic topic = topicService.findById(topicId);
                bookReviewVo.setTopic(topic);
            }
            LitemallGoodsProduct goodsProduct = goodsProductService.findById(bookReview.getGoodsProductId());
            if (goodsProduct != null) {
                bookReviewVo.setGoodsProduct(goodsProduct);
                LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
                bookReviewVo.setGoods(goods);
                bookReviewVoList.add(bookReviewVo);
            }
        }
        return bookReviewVoList;
    }

    public LitemallBookReview findById(Integer id) {
        return bookReviewMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer goodsProductId) {
        LitemallBookReviewExample example = new LitemallBookReviewExample();
        example.or().andGoodsProductIdEqualTo(goodsProductId);
        bookReviewMapper.deleteByExample(example);
    }
}
